class AsyncSeriesBailHook {
    events = [];
    promiseEvents = [];

    constructor() {
        console.log('AsyncSeriesHook');
    }

    tapAsync(name, cb) {
        cb = typeof cb === 'function' ? cb : (...args) => args;
        this.events.push(cb);
    }

    tapPromise(name, cb) {
        cb = typeof cb === 'function' ? cb : (...args) => args;
        this.promiseEvents.push(cb);
    }

    callAsync(...args) {
        const callAsyncEvent = args.pop();
        const event = this.events.shift();
        const next = (err, data) => {
            if (err === null || err === undefined || err === false) {
                callAsyncEvent && callAsyncEvent(null, data);
            } else {
                callAsyncEvent && callAsyncEvent(err, null);
            }
        }
        event(...args, next);
    }

    promise(...args) {
        return this.promiseEvents[0](...args);
    }
}

module.exports = AsyncSeriesBailHook;